package br.inatel.ec203.projeto.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JPanel;

public class OkCancelButtonsPanel extends JPanel{
	
	private static final long serialVersionUID = -6278417531935613265L;
	
	private JButton okButton;
	private JButton cancelButton;
	
	private ArrayList<OkCancelButtonsListeners> listeners = new ArrayList<OkCancelButtonsListeners>();
	
	public OkCancelButtonsPanel() {
		super();
		
		initialize();		
	}

	private void initialize() {
		add(getOkButton());
		add(getCancelButton());
	}

	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("OK");
			okButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {					
					for (OkCancelButtonsListeners listener : listeners) {
						listener.okPerformed();
					}
				}
			});
		}
		return okButton;
	}

	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
			cancelButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					for (OkCancelButtonsListeners listener : listeners) {
						listener.cancelPerformed();
					}
				}
			});
		}
		return cancelButton;
	}
	
	public void addOkCancelButtonsListeners(OkCancelButtonsListeners listener) {
		this.listeners.add(listener);
	}
	
}
